﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi.PostModel;
using WebApi.Models;
using WebApi.Class;
using Yd.Domain.DAL;
using Yd.BLL;
using System.Data;
using System.Data.SqlClient;
using Yd.Model;
using System.Text;
namespace WebApi.Controllers
{
    public class OrderController : ApiController
    {
        public CommDAL commdal = new CommDAL();
        // Post 用户登录 api/user/login
        /// <returns>登录成功返回userinfo  -1异常 -4 参数错误</returns>
        public object InsertOrder([FromBody]PostSumbitOrder post)
        {
            ModelBase mbase = new ModelBase();
            if (post.userId < 1 || post.shopId < 1 || post.kzId < 1 || StringUtils.getIsEmpty(post.kzName) || StringUtils.getIsEmpty(post.arrivalTime))
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }
            SqlParameter[] fparam = new SqlParameter[] { 
                new SqlParameter("@uid",post.userId),
                new SqlParameter("@shopid",post.shopId),
                new SqlParameter("@kzid",post.kzId),
                new SqlParameter("@kzName",post.kzName),
                new SqlParameter("@arrivalTime",post.arrivalTime)
            };
            int _fresult = -1;
            _fresult = commdal.UpdateDataByPro("InsertOrder", fparam);
            if (_fresult > 0)
            {
                DataSet ds1 = commdal.QueryObjectAllSql("select top 1 * from yd_formid where oid=" + _fresult + " and isSend=0 order by createTime desc");
                if (ds1 != null && ds1.Tables[0].Rows.Count > 0)
                {
                    //查找到了需要可以發送的form
                    DataSet dsorder = commdal.QueryObjectAllSql("select top 1 * from yd_order where oid=" + _fresult);
                    if (dsorder != null && dsorder.Tables[0].Rows.Count > 0)
                    {
                        WeChatAppDecrypt chatapp = new WeChatAppDecrypt("wxd219d8ad073adeaa", "9de5bdb840330ef17f335ba179254cc7");
                        int sendret = chatapp.SendOrderNotice(dsorder, ds1.Tables[0].Rows[0]["openid"].ToString(), ds1.Tables[0].Rows[0]["wxFormid"].ToString());
                        if (sendret == 0) {
                            //修改formid 状态
                            commdal.SaveOrUpdateSql("update yd_formid set isSend=1 where fid=" + ds1.Tables[0].Rows[0]["fid"]);
                            mbase.code = 0;
                            mbase.message = "申请成功,等待商家确认!";
                            return mbase;
                        }
                        mbase.code = sendret;
                        mbase.message = ErrorCodeUtil.getCodeMessage(sendret);
                        return mbase;
                    }
                    else {
                        mbase.code = -1;
                        mbase.message = "操作失败";
                        return mbase;
                    }
                }
                else {
                    mbase.code = -7;
                    mbase.message = ErrorCodeUtil.getCodeMessage(-7);
                    return mbase;
                }
            }
            mbase.code = _fresult;
            mbase.message = ErrorCodeUtil.getCodeMessage(_fresult);
            return mbase;
        }

        ///提交活动票订单
        public object InsertOrderFromTicket([FromBody]PostSumbitOrderFromTicket post)
        {
            ModelBase mbase = new ModelBase();
            if (post.userId < 1 || post.tid < 1 || post.ticketNum < 1)
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }
            SqlParameter[] fparam = new SqlParameter[] { 
                new SqlParameter("@uid",post.userId),
                new SqlParameter("@tickId",post.tid),
                new SqlParameter("@tickNum",post.ticketNum)
            };
            int _fresult = -1;
            _fresult = commdal.UpdateDataByPro("InsertOrderFromTicket", fparam);
            if (_fresult > 0)
            {
                DataSet dsorder = commdal.QueryObjectAllSql("select top 1 * from yd_order where oid=" + _fresult);
                if (dsorder != null && dsorder.Tables[0].Rows.Count > 0)
                {
                    //查询到订单信息
                    OrderModel model = OrderModel.GetModel(dsorder.Tables[0].Rows[0]);
                    if (model != null)
                    {
                        mbase.code = 0;
                        mbase.data = model;
                        mbase.message = ErrorCodeUtil.getCodeMessage(0);
                    }
                    else
                    {
                        mbase.code = -1;
                        mbase.message = ErrorCodeUtil.getCodeMessage(-1);
                    }
                    return mbase;
                }
                else
                {
                    mbase.code = -1;
                    mbase.message = "操作失败";
                    return mbase;
                }
            }
            mbase.code = _fresult;
            mbase.message = ErrorCodeUtil.getCodeMessage(_fresult);
            return mbase;
        }

        /// Post 獲取订单列表 api/order/orderList
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object orderList([FromBody]PostOrderList post)
        {
            ModelBase mbase = new ModelBase();
            if (post.userId < 1 && post.shopId < 1 )
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }
            string pagewhere = "";
            if (post.pageSize == 0)
            {
                post.pageSize = 10;
            }
            string orderno = "";
            if (!StringUtils.getIsEmpty(post.orderNo)) {
                orderno = post.orderNo;
            }
            SqlParameter[] fparam = new SqlParameter[] { 
                new SqlParameter("@uid",post.userId),
                new SqlParameter("@shopid",post.shopId),
                new SqlParameter("@orderno",orderno),
                new SqlParameter("@orderStatus",post.orderStatus-1),
                new SqlParameter("@PageSize",post.pageSize),
                new SqlParameter("@PageIndex",post.pageIndex)
            };
            int r;
            DataSet dsorder=commdal.QueryView_yd_order(fparam, post.pageSize, post.pageIndex, out r);
            if (dsorder != null)
            {
                mbase.code = 0;
                mbase.data = dsorder.Tables[0];
                mbase.message = "获取成功";
                if(post.userId==0){
                    DataSet ds1 = commdal.QueryObjectAllSql("select count(oid) as num from yd_order where shopid=" + post.shopId + " and orderStatus=" + (post.orderStatus - 1));
                     if (ds1 != null && ds1.Tables[0].Rows.Count > 0) {
                         mbase.total = Convert.ToInt32(ds1.Tables[0].Rows[0]["num"]);
                     }
                }
               
            }
            else
            {
                mbase.code = -1;
                mbase.message = ErrorCodeUtil.getCodeMessage(-1);
            }
            return mbase;
        }

        /// Post 接单或拒接 api/kazuo/UpdateOrderStatus
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object UpdateOrderStatus([FromBody]PostUpdateOrder post)
        {
            ModelBase mbase = new ModelBase();
            if (post.oid == 0||(post.orderStatus!=1&&post.orderStatus!=2))
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }

            SqlParameter[] fparam = new SqlParameter[] { 
                new SqlParameter("@oid",post.oid),
                new SqlParameter("@orderStatus",post.orderStatus)
            };
            int _fresult = -1;
            _fresult = commdal.UpdateDataByPro("UpdateOrder", fparam);
            if (_fresult == 0)
            {
                mbase.code = 0;
                mbase.message = "处理订单成功";
                return mbase;
            }
            else
            {
                mbase.code = _fresult;
                mbase.message = ErrorCodeUtil.getCodeMessage(_fresult);
            }
            return mbase;
        }

        /// Post 使用活动票修改订单状态 api/order/UpdateOrderStatusByTicket
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object UpdateOrderStatusByTicket([FromBody]PostSyTicketCode post)
        {
            ModelBase mbase = new ModelBase();
            if (StringUtils.getIsEmpty(post.code))
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }

            SqlParameter[] fparam = new SqlParameter[] { 
                new SqlParameter("@tickcode",post.code)
            };
            int _fresult = -1;
            _fresult = commdal.UpdateDataByPro("UpdateTicketCodeStatus", fparam);
            if (_fresult == 0)
            {
                mbase.code = 0;
                mbase.message = "处理成功";
                return mbase;
            }
            else
            {
                mbase.code = _fresult;
                mbase.message = ErrorCodeUtil.getCodeMessage(_fresult);
            }
            return mbase;
        }

        /// Post 改变卡座预定情况 api/kazuo/shopList
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object OverOrderConsumption([FromBody]PostOverOrder post)
        {
            ModelBase mbase = new ModelBase();
            if (post.oid == 0)
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }

            SqlParameter[] fparam = new SqlParameter[] { 
                new SqlParameter("@oid",post.oid)
            };
            int _fresult = -1;
            _fresult = commdal.UpdateDataByPro("UpdateOrderConsumptionToOver", fparam);
            if (_fresult == 0)
            {
                mbase.code = 0;
                mbase.message = "该订单已结束";
                return mbase;
            }
            else
            {
                mbase.code = _fresult;
                mbase.message = ErrorCodeUtil.getCodeMessage(_fresult);
            }
            return mbase;
        }

        /// Post 獲取订单下活动票 api/order/orderTictetist
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object orderTicketList([FromBody]PostOrderTicketList post)
        {
            ModelBase mbase = new ModelBase();
            if (post.oid < 1)
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }
            string sql = String.Format(@"SELECT *
  FROM [dbo].[yd_ticket_code] where oid={0}", post.oid);
            int r;
            DataSet dsorder = commdal.QueryObjectAllSql(sql);
            if (dsorder != null)
            {
                mbase.code = 0;
                mbase.data = dsorder.Tables[0];
                mbase.message = "获取成功";
            }
            else
            {
                mbase.code = -1;
                mbase.message = ErrorCodeUtil.getCodeMessage(-1);
            }
            return mbase;
        }
    }
}
